package com.murray.assignment.service.impl;

import com.murray.assignment.mapper.*;
import com.murray.assignment.pojo.Address;
import com.murray.assignment.pojo.CartInfo;
import com.murray.assignment.pojo.OrderDetails;
import com.murray.assignment.pojo.Orders;
import com.murray.assignment.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Murray
 * @Date 2021/8/13
 * @Time 15:23
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private OrderDetailsMapper orderDetailsMapper;

    @Autowired
    private CartInfoMapper cartInfoMapper;

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public Integer addOrder(Orders orders) {
        //1.添加订单信息到orders表，并且返回订单id
        ordersMapper.insertOrder(orders);
        //2.添加订单详情到orderDetails表
        Map<String, Object> map = new HashMap<>();
        map.put("userId", orders.getUserId());
        map.put("list", orders.getOrderDetailsList());
        List<OrderDetails> orderDetailsList = orderDetailsMapper.selectOrderDetailsList(map);

        Map<String, Object> map1 = new HashMap<>();
        map1.put("orderId", orders.getOrderId());
        map1.put("list", orderDetailsList);
        orderDetailsMapper.insertOrderDetails(map1);
        return orders.getOrderId();
    }

    @Override
    public Integer addOrder(Orders orders, Integer cartId) {
        //1.添加订单信息到orders表，并且返回订单id
        ordersMapper.insertOrder(orders);
        //2.添加订单详情到orderDetails表
        Map<String, Object> map = new HashMap<>();
        map.put("userId", orders.getUserId());
        map.put("list", orders.getOrderDetailsList());
        List<OrderDetails> orderDetailsList = orderDetailsMapper.selectOrderDetailsList(map);

        Map<String, Object> map1 = new HashMap<>();
        map1.put("orderId", orders.getOrderId());
        map1.put("list", orderDetailsList);
        orderDetailsMapper.insertOrderDetails(map1);

        //删除购物车中的商品,用之前的代码了
        for (OrderDetails orderDetails : orders.getOrderDetailsList()) {
            CartInfo cartInfo = new CartInfo();
            cartInfo.setCartId(cartId);
            cartInfo.setSkuId(orderDetails.getSkuId());
            cartInfoMapper.deleteGoodsFromCart(cartInfo);
            cartMapper.subCart(cartId);
        }

        return orders.getOrderId();
    }

    /**
     * 修改订单状态为已经付款的状态
     *
     * @param orderId 传入订单号id
     * @return 受影响的行数
     */
    @Override
    public Integer updateOrderState(Integer orderId) {
        return ordersMapper.updateOrder(orderId);
    }

    @Override
    public Address selectAddressByOrderId(Integer orderId) {
        return addressMapper.selectAddressByOrderId(orderId);
    }

    @Override
    public List<Orders> selectOrdersByUserId(Integer userId) {
        return ordersMapper.selectOrderList(userId);
    }


}
